/**
 * 
 */
package edu.gtcfla.km.admin.appmodules.web;

import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.vsg.common.i18n.I18nMessageService;
import org.vsg.common.web.json.ListResult;
import org.vsg.common.web.json.processor.JsDateViewJsonValueProcessor;
import org.vsg.security.annotation.RegisterRequestPermissionStrategy;

import edu.gtcfla.km.admin.appmodules.service.FunctionManagerService;
import edu.gtcfla.km.admin.appmodules.service.MenuManagerService;
import edu.gtcfla.km.admin.appmodules.service.ModuleManagerService;
import edu.gtcfla.km.admin.appmodules.service.UrlManagerService;
import edu.gtcfla.km.admin.appmodules.vo.HierarchyMenuItem;
import edu.gtcfla.km.admin.appmodules.vo.UrlInModuleVO;
import edu.gtcfla.km.core.ApplicationConstant;

/**
 * @author Bill vison
 *
 */
@Controller
@Lazy
@RequestMapping(ApplicationConstant.DATA_RERESOURCE)
public class FunInModuleController {
	
	private final static Logger logger = LoggerFactory.getLogger(FunInModuleController.class);	

	
	private String modulePrefix = ApplicationConstant.DATA_RERESOURCE + "/resources";
	
	@Autowired(required =false)
	private ModuleManagerService moduleManagerService;
	
	@Autowired(required =false)
	private I18nMessageService i18nMessageService;	
	
	@Autowired(required =false)	
	private FunctionManagerService moduleFunctionService;
	

	@Autowired(required =false)	
	private MenuManagerService menuManagerService;
	
	@Autowired(required =false)	
	private UrlManagerService UrlManagerService;
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = "tree_menusInModule.json",
		skipCheck = true
	)
	@RequestMapping(value = ApplicationConstant.DATA_RERESOURCE + "/tree_menusInModule.json")
	public ModelAndView accessOfTreeMenusInModule(String funId)
			throws Exception {

		
		Set<HierarchyMenuItem> menuItems = null;
	
		if (funId != null) {
			menuItems =  menuManagerService.getMenuTreeInFunction( funId );
		}

		
		JsonConfig jcfg = new JsonConfig();	
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONArray jsonObject = JSONArray.fromObject(menuItems, jcfg);	
	


		
		ModelAndView mav = new ModelAndView();
		mav.addObject("json", jsonObject);
		mav.setViewName("result"); // view name
        
		return mav;

	}

	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = "list_urlsInModule.json",
		skipCheck = true
	)	
	@RequestMapping(value = ApplicationConstant.DATA_RERESOURCE + "/list_urlsInModule.json")
	public ModelAndView accessOfUrlsInModuleList(String funId)
			throws Exception {

		// --- map to json object data ---
		
		Set<UrlInModuleVO> allUrls = UrlManagerService.findUrlInFunction(funId);

		ListResult listResult = new ListResult();
		listResult.setRows( allUrls );
		listResult.setResults( allUrls.size() );
		
		JsonConfig jcfg = new JsonConfig();	
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(listResult, jcfg);		

		ModelAndView mav = new ModelAndView();
		mav.addObject("json", jsonObject);
		mav.setViewName("result"); // view name

        
		return mav;

	}

	
}
